<?php

namespace Common\Model;

use Think\Model;

/**
 * 首页数据模型，当前为直接从数据库取数据，待后期数据量起来了再将设为推荐
 */
class IndexModel extends Model {

    private static $default = [
        'count' => 0,
        'list' => []
    ];

    /**
     * 获取首页的文档列表数据
     * @param $c
     * @param $p
     * @param $pcount
     * @return array|bool
     */
    public static function getDocList($c, $p, $pcount){
        $where = [
            'status' => DocModel::NORMAL
        ];
        $order = 'collect desc';
        $page = $p.','.$pcount;
        if ($c < 0) {
            return self::$default;
        } else if ($c == 0) {    // 首页所有分类
            $count = M('Doc')->where($where)->count();
            $list = $count ? M('Doc')->where($where)->order($order)->page($page)->select() : [];
        } else {
            $class = ClassModel::getClassById($c);
            if(!$class || $class['status'] == ClassModel::DELTEL){
                return self::$default;
            }
            if($class['pid'] == 0){
                $classList = ClassModel::getClassSons($c);
                if(!$classList){
                    return self::$default;
                }
                $where['class'] = ['in', $classList];
                $count = M('Doc')->where($where)->count();
                $list = $count ? M('Doc')->where($where)->order($order)->page($page)->select() : [];
            }else{
                $where['class'] = $c;
                $count = M('Doc')->where($where)->count();
                $list = $count ? M('Doc')->where($where)->order($order)->page($page)->select() : [];
            }
        }
        if ($list) {
            foreach ($list as $key => $value) {
                if ($addData = DocCountModel::get($value['id'])) {
                    foreach ($addData as $k => $v) {
                        $list[$key][$k] += $v;
                    }
                }
                if ($value['bid']) {
                    $string = '<a href="//bbs.wooo.wang/'.$value['bid'].'" class="info-org">'.BbsModel::getBbsName($value['bid']).'</a>';
                } else {
                    $string = '<a href="//wooo.wang/u/'.$value['uid'].'" class="info-org">'.UserModel::getUserName($value['uid']).'</a>';
                }
                $list[$key]['author'] = $string;
            }
        }
        return [
            'count' => $count,
            'list' => $list
        ];
    }

    /**
     * 获取首页的团体列表数据
     * @param $c
     * @param $p
     * @param $pcount
     * @return array|bool
     */
    public static function getBbsList($c, $p, $pcount){
        $where = [
            'status' => DocModel::NORMAL
        ];
        $order = 'gx desc';
        $page = $p.','.$pcount;
        if ($c < 0) {
            return self::$default;
        } else if ($c == 0) {    // 首页所有分类
            $count = M('Bbs')->where($where)->count();
            $list = $count ? M('Bbs')->where($where)->order($order)->page($page)->select() : [];
        } else {
            $class = ClassModel::getClassById($c);
            if(!$class || $class['status'] == ClassModel::DELTEL){
                return self::$default;
            }
            if($class['pid'] == 0){
                $classList = ClassModel::getClassSons($c);
                if(!$classList){
                    return self::$default;
                }
                $where['class'] = ['in', $classList];
                $count = M('Bbs')->where($where)->count();
                $list = $count ? M('Bbs')->where($where)->order($order)->page($page)->select() : [];
            }else{
                $where['class'] = $c;
                $count = M('Bbs')->where($where)->count();
                $list = $count ? M('Bbs')->where($where)->order($order)->page($page)->select() : [];
            }
        }
        if ($list) {
            foreach ($list as $key => $value) {
                if ($addData = BbsCountModel::get($value['id'])) {
                    foreach ($addData as $k => $v) {
                        $list[$key][$k] += $v;
                    }
                }
            }
        }
        return [
            'count' => $count,
            'list' => $list
        ];
    }

}